package com.qfedu.wmsuser.user.controller;



import com.qfedu.common.core.common.R;
import com.qfedu.common.core.constant.CommonConstant;
import com.qfedu.common.core.entity.UserStaff;
import com.qfedu.common.core.utils.TokenUtils;
import com.qfedu.wmsuser.user.service.UserService;
import com.qfedu.wmsuser.user.vo.UserStaffVO;
import com.qfedu.wmsuser.user.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zsjx
 * 2024/12/26 下午4:29
 * com.qfedu.microuser.user.controller
 * micro2405
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取 “我的” 信息，不需要传用户id
     * 用户id用过token获取
     * UserStaffVO userStaffVO
     */
//    @NoLoginCheck
    @RequestMapping("/getInfo")
    public Mono<R> my2(ServerHttpRequest request) {
        log.info("获取我的信息 my1 执行了");
        return userService.my2(request)
                .map(userStaffVO -> R.ok("my1 获取我的信息成功", userStaffVO))
                .onErrorResume(e -> Mono.just(R.fail(e.getMessage())));
    }

    /**
     * 获取 “我的” 信息，不需要传用户id
     * 用户id用过token获取
     */
//    @NoLoginCheck
    @RequestMapping("/getInfo2")
    public Mono<R> my1(ServerHttpRequest request) {
        log.info("获取我的信息 my2 执行了");
        return userService.my1(request)
                .map(userVO -> R.ok("my2 获取我的信息成功", userVO))
                .onErrorResume(e -> Mono.just(R.fail(e.getMessage())));
    }

    @RequestMapping("/getInfo1")
    public R my() {
        log.info("获取我的信息 my 执行了");
        UserVO userVO = userService.my();
        return R.ok("my 获取我的信息成功",userVO);
    }

    /**
     * 登出
     */
    @RequestMapping("/logout")
    public Mono<R> logout(ServerHttpRequest request) {
        log.info("User is trying to logout");
        return userService.logout(request);
    }

    /**
     * 添加用户
     */
    @RequestMapping("/add")
    public R add(@RequestBody UserStaff userStaff) {
        // 非空校验
        if (userStaff == null) {
            return R.fail("添加用户失败：信息不能为空");
        }
        log.info("添加用户 add 执行了");
        int i = userService.insertUserStaff(userStaff);
        if (i == 0) {
            return R.fail("添加用户失败");
        }
        return R.ok("添加用户成功");
    }

    /**
     * 修改用户
     * @param userStaff 用户信息
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserStaff userStaff) {
        log.info("userStaff: {}", userStaff);
        // 非空校验
        if (userStaff == null) {
            return R.fail("修改用户失败：信息不能为空");
        }
        log.info("修改用户 update 执行了");
        int i = userService.updateUserStaff(userStaff);
        if (i == 0) {
            return R.fail("修改用户失败");
        }
        return R.ok("修改用户成功");
    }

    /**
     * 删除用户
     * @param userStaffId 用户id
     */
    @GetMapping("/delete")
    public R delete(Integer userStaffId) {
        // 非空校验
        if (userStaffId == null) {
            return R.fail("删除用户失败：信息不能为空");
        }
        log.info("删除用户 delete 执行了");
        log.info("userStaffId: {}", userStaffId);
        int i = userService.deleteById(userStaffId);
        if (i == 0) {
            return R.fail("删除用户失败");
        }
        return R.ok("删除用户成功");
    }

    /**
     * 头像上传
     */
    @RequestMapping("/upload/avatar")
    public R upload(MultipartFile file) {
        log.info("头像上传 upload 执行了");
        userService.uploadAvatar(file);
        return R.ok("上传成功");
    }

}
